package com.egao.appo.biz.impl;

import com.egao.common.core.damon.constant.UserConstant;
import com.egao.common.core.damon.results.BaseDao;
import com.egao.common.core.damon.results.PageResult2;
import com.egao.common.core.damon.utils.Utils;
import com.egao.common.system.entity.User;
import com.egao.appo.biz.IntroductionBiz;
import com.egao.appo.entity.Introduction;
import com.egao.common.core.web.JsonResult;
import com.egao.common.core.damon.utils.DateUtil;
import org.apache.poi.ss.formula.functions.T;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.stereotype.Repository;

import java.util.HashMap;
import java.util.Map;
import java.util.List;

/**
 *
 * 数据层实现类
 * @author : Doman
 * @date : 2021-07-23 16:57:31
 * @version ：1.0
 */
@Repository
public class IntroductionBizImpl extends BaseDao implements IntroductionBiz {

	@Override
	public JsonResult list(PageResult2<T> page, Introduction introduction, User loginUser) throws Exception {
		Map<String, String> map  = new HashMap<String, String>();
		RowMapper<Introduction> rm = new BeanPropertyRowMapper<Introduction>(Introduction.class);
		StringBuffer sql = new StringBuffer("select i.id,i.organization_id,i.appo_Id");
		sql.append(",i.content,i.operate_user_id,i.remark,i.intro_date");
		sql.append(",i.order_num,i.create_time,i.create_user,i.update_time");
		sql.append(",i.update_user,operater.nickname operateUserName,app.name appoName");
		sql.append(",sogzn.organization_name organizationName,suser.username createName ");
		sql.append(" from fx_introduction i ");
		sql.append(" inner join fx_appointments app on app.id=i.appo_Id ");
		sql.append(" inner join sys_user suser on suser.user_id=i.create_user ");
		sql.append(" inner join sys_user operater on operater.user_id=i.operate_user_id ");
		sql.append(" inner join sys_organization sogzn on sogzn.organization_id=i.organization_id and sogzn.deleted=0 ");
		sql.append(" where i.deleted=0 ");
		if(loginUser.getUserId() != UserConstant.ADMIN_ID){
			sql.append(" and i.organization_id=:tableOrganizationId ");
			map.put("tableOrganizationId", loginUser.getOrganizationId() + "");
		}
		if(Utils.isNotEmpty(introduction)){
			if(Utils.isNotEmpty(introduction.getAppoName())){
				sql.append(" and instr(app.name,:appoName)>0 ");
				map.put("appoName", introduction.getAppoName());
			}
			if(Utils.isNotEmpty(introduction.getSort())&&Utils.isNotEmpty(introduction.getOrder())){
				sql.append(" order by i."+introduction.getSort()+" "+introduction.getOrder());
			} else {
				sql.append(" order by i.create_time desc");
			}
		}
		return jsonResult(getPageQueryList(page, sql.toString(), map, rm));
	}

	@Override
	public List<Introduction> list(Introduction introduction, User loginUser) {
		Map<String, String> map  = new HashMap<String, String>();
		RowMapper<Introduction> rm = new BeanPropertyRowMapper<Introduction>(Introduction.class);
		StringBuffer sql = new StringBuffer("select i.id,i.organization_id,i.appo_Id");
		sql.append(",i.content,i.operate_user_id,i.remark,i.intro_date");
		sql.append(",i.order_num,i.create_time,i.create_user,i.update_time");
		sql.append(",i.update_user,operater.nickname operateUserName,app.name appoName");
		sql.append(",sogzn.organization_name organizationName,suser.username createName ");
		sql.append(" from fx_introduction i ");
		sql.append(" inner join fx_appointments app on app.id=i.appo_Id ");
		sql.append(" inner join sys_user suser on suser.user_id=i.create_user ");
		sql.append(" inner join sys_user operater on operater.user_id=i.operate_user_id ");
		sql.append(" inner join sys_organization sogzn on sogzn.organization_id=i.organization_id and sogzn.deleted=0 ");
		sql.append(" where i.deleted=0 ");
		if(loginUser.getUserId() != UserConstant.ADMIN_ID){
		sql.append(" and i.organization_id=:tableOrganizationId ");
		map.put("tableOrganizationId", loginUser.getOrganizationId() + "");
    }
    if(Utils.isNotEmpty(introduction)){
//			if(Utils.isNotEmpty(introduction.getName())){
//				sql.append(" and instr(i.name,:name)>0 ");
//				map.put("name", introduction.getName());
//			}
      if(Utils.isNotEmpty(introduction.getSort())&&Utils.isNotEmpty(introduction.getOrder())){
				sql.append(" order by i."+introduction.getSort()+" "+introduction.getOrder());
			} else {
				sql.append(" order by i.create_time desc");
			}
		}
		getMapParameter(sql, map);
		return jdbcTemplate.query(sql.toString(),map, rm);
	}

	@Override
	public Introduction getById(Introduction introduction) {
		RowMapper<Introduction> rm = new BeanPropertyRowMapper<Introduction>(Introduction.class);
		Map<String, String> map  = new HashMap<String, String>();
		StringBuffer sql = new StringBuffer("select i.id,i.organization_id,i.appo_Id");
    	sql.append(",i.content,i.operate_user_id,i.remark,i.intro_date");
		sql.append(",i.order_num,i.create_time,i.create_user,i.update_time");
		sql.append(",i.update_user,operater.nickname operateUserName,app.name appoName");
    	sql.append(",sogzn.organization_name organizationName,suser.username createName ");
    	sql.append(" from fx_introduction i ");
		sql.append(" inner join fx_appointments app on app.id=i.appo_Id ");
		sql.append(" inner join sys_user suser on suser.user_id=i.create_user ");
		sql.append(" inner join sys_user operater on operater.user_id=i.operate_user_id ");
		sql.append(" inner join sys_organization sogzn on sogzn.organization_id=i.organization_id and sogzn.deleted=0 ");
		sql.append(" where i.deleted=0 and i.id=:id ");
		map.put("id", introduction.getId());
		getMapParameter(sql, map);
		return jdbcTemplate.queryForObject(sql.toString(), map, rm);
	}

	@Override
	public boolean save(Introduction introduction, User loginUser) {
		Map<String, String> map  = new HashMap<String, String>();
		StringBuffer sql =new StringBuffer("insert into fx_introduction (");
    	sql.append("organization_id,appo_Id,content,operate_user_id");
		sql.append(",remark,intro_date,order_num,create_user,create_time) values ( ");
    	sql.append(":organization_id,:appo_Id,:content,:operate_user_id");
		sql.append(",:remark,:intro_date,:order_num,:create_user,now())");
		map.put("organization_id", "1");
		map.put("appo_Id", introduction.getAppoId()+"");
		map.put("content", introduction.getContent());
		map.put("operate_user_id", loginUser.getUserId()+"");
		map.put("remark", introduction.getRemark());
		map.put("intro_date", introduction.getIntroDate());
		map.put("order_num", introduction.getOrderNum()==null?null:introduction.getOrderNum()+"");
		map.put("create_user", loginUser.getUserId() + "");
		getMapParameter(sql, map);
		int count = jdbcTemplate.update(sql.toString(), map);
		return count > 0;
	}

	@Override
	public boolean update(Introduction introduction, User loginUser) {
		Map<String, String> map  = new HashMap<String, String>();
		StringBuffer sql =new StringBuffer("update fx_introduction set ");
    	sql.append("appo_Id=:appo_Id,content=:content,operate_user_id=:operate_user_id ");
		sql.append(",remark=:remark,intro_date=:intro_date,order_num=:order_num ");
		sql.append(",update_time=now(),update_user=:update_user where id=:id ");
		map.put("appo_Id", introduction.getAppoId()==null?null:introduction.getAppoId()+"");
		map.put("content", introduction.getContent());
		map.put("operate_user_id", introduction.getOperateUserId()==null?null:introduction.getOperateUserId()+"");
		map.put("remark", introduction.getRemark());
		map.put("intro_date", introduction.getIntroDate());
		map.put("order_num", introduction.getOrderNum()==null?null:introduction.getOrderNum()+"");
		map.put("update_user", loginUser.getUserId() + "");
		map.put("id", introduction.getId());
		getMapParameter(sql, map);
		int count = jdbcTemplate.update(sql.toString(), map);
		return count > 0;
	}

	@Override
	public boolean remove(Introduction introduction, User loginUser) {
		int count = 0;
		Map<String, String> map = null;
		if(introduction.getId().length()>0){
			for(String ID : introduction.getId().split(",")){
				map  = new HashMap<String, String>();
				if(ID==null || ID.equals("")){ break; }
				StringBuffer sql =new StringBuffer("update fx_introduction set deleted=1" +
						",update_user=:update_user, update_time=now() "
						+ " where id =:id ");
				map.put("update_user", loginUser.getUserId() + "");
				map.put("id", ID);
				getMapParameter(sql, map);
				count = jdbcTemplate.update(sql.toString(),map);
			}
		}
		return count > 0;
	}
}
